import 'dart:io';

void main() async {
  print('🔧 修复所有语法错误...');
  
  final filesToFix = [
    'lib/data/esperanto_mega_vocabulary.dart',
    'lib/data/esperanto_vocabulary_batch2.dart',
  ];
  
  for (final filePath in filesToFix) {
    print('修复文件: $filePath');
    await fixAllSyntax(filePath);
  }
  
  print('✅ 修复完成！');
}

Future<void> fixAllSyntax(String filePath) async {
  final file = File(filePath);
  if (!await file.exists()) {
    print('❌ 文件不存在: $filePath');
    return;
  }
  
  String content = await file.readAsString();
  
  // 修复所有语法问题
  content = content
    // 修复缺失的右方括号
    .replaceAllMapped(
      RegExp(r"examples: \['([^']*\([^']*)'([^']*)'([^']*)\), category:"),
      (match) => "examples: ['${match.group(1)}\\'${match.group(2)}\\'${match.group(3)}'], category:"
    )
    // 修复单引号问题
    .replaceAll("Don\']t", "Don\\'t")
    .replaceAll("I don\']t", "I don\\'t")
    .replaceAll("doesn\']t", "doesn\\'t")
    .replaceAll("can\']t", "can\\'t")
    .replaceAll("won\']t", "won\\'t")
    .replaceAll("isn\']t", "isn\\'t")
    .replaceAll("aren\']t", "aren\\'t")
    .replaceAll("wasn\']t", "wasn\\'t")
    .replaceAll("weren\']t", "weren\\'t")
    .replaceAll("haven\']t", "haven\\'t")
    .replaceAll("hasn\']t", "hasn\\'t")
    .replaceAll("hadn\']t", "hadn\\'t")
    .replaceAll("wouldn\']t", "wouldn\\'t")
    .replaceAll("shouldn\']t", "shouldn\\'t")
    .replaceAll("couldn\']t", "couldn\\'t");
  
  // 手动修复具体的错误行
  final fixes = [
    // mega vocabulary 修复
    [r"examples: \['Ne tuŝu la varman fornon \(Don\\'t touch the hot stove\)', category:", "examples: ['Ne tuŝu la varman fornon (Don\\'t touch the hot stove)'], category:"],
    [r"examples: \['Ne kriu en la biblioteko \(Don\\'t shout in the library\)', category:", "examples: ['Ne kriu en la biblioteko (Don\\'t shout in the library)'], category:"],
    // batch2 修复
    [r"examples: \['Mi ne havas tempon \(I don\\'t have time\)', category:", "examples: ['Mi ne havas tempon (I don\\'t have time)'], category:"],
    [r"examples: \['Mi ne manĝas viandon \(I don\\'t eat meat\)', category:", "examples: ['Mi ne manĝas viandon (I don\\'t eat meat)'], category:"],
  ];
  
  for (final fix in fixes) {
    content = content.replaceAll(RegExp(fix[0]), fix[1]);
  }
  
  await file.writeAsString(content);
  print('✅ 已修复: $filePath');
} 